<?php defined('SYSPATH') or die('No direct script access.');

/**游记
 * Class Controller_Pc_Api_V2_Notes
 */
class Controller_Pc_Api_V2_Notes extends Controller_Pc_Api_v2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Notes
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Notes
     */
    private $service;


    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('notes', $this->request_params);

        // 服务
        $this->service = new Service_Api_Notes();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 列表
    public function action_get_list()
    {
        try {
            $this->validation->get_list()->check();

            $params = array(
                'page' => Arr::get($this->request_params, 'page', 1),
                'pagesize' => Arr::get($this->request_params, 'pagesize', 10),
                'keyword' => Arr::get($this->request_params, 'keyword', ''),
                'status' => 1,
                'dest_id' => '',
                'mid' => ''
            );

            $this->result['data'] = $this->service->get_list($params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 详情
    public function action_get_detail()
    {
        try {
            $this->validation->get_detail()->check();

            $this->result['data'] = $this->service->get_detail($this->request_params['id']);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 我的游记
    public function action_member_notes()
    {
        try {
            $this->validation->member_notes()->check();

            $params = array(
                'page' => Arr::get($this->request_params, 'page', 1),
                'pagesize' => Arr::get($this->request_params, 'pagesize', 10),
            );

            $this->result['data'] = $this->service->member_notes($this->mid, $params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 点赞
    public function action_thumbs_up()
    {
        try {
            $this->validation->thumbs_up()->check();

            $this->result['data']['status'] = $this->service->thumbs_up($this->request_params['id']);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    public function action_thumbs_down()
    {
        try {
            $this->validation->thumbs_down()->check();

            $this->result['data']['status'] = $this->service->thumbs_down($this->request_params['id']);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 删除游记
    public function action_delete()
    {
        try {
            $this->validation->delete()->check();

            $this->result['data']['status'] = $this->service->delete($this->mid, $this->request_params['id']);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    /**
     * 发布游记
     */
    public function action_edit()
    {
        //开启事务
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->edit()->check();

            $captcha_key = Arr::get($this->request_params, 'captcha_key');
            $code = Arr::get($this->request_params, 'code');
            if (!Model_Api_V2_Common::verify_image_code($captcha_key, $code)) {
                throw new Exception('图片码错误');
            }

            $phone = Arr::get($this->request_params, 'phone');
            $sms_key = 'uni_note_msg_code_' . $phone;
            $sms_code = Model_Api_V2_Common::cache('get', $sms_key);
            if (empty($sms_code)) {
                throw new Exception('请先获取短信验证码');
            }

            $msg_code = Arr::get($this->request_params, 'msg_code');
            if ($sms_code != $msg_code) {
                throw new Exception('短信码错误');
            }

            $this->result['data']['status'] = $this->service->edit($this->mid, $this->request_params);

            Model_Api_V2_Common::cache('set', $captcha_key, null);
            Model_Api_V2_Common::cache('set', $sms_key, null);

            $db->commit();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();

            $db->rollback();
        }
    }




}